/*
 * Copyright 2009 Johnson Consulting Services
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * 
 */
package com.johnsoncs.gwt.application.client.validation.impl;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.HasBlurHandlers;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.johnsoncs.gwt.application.client.validation.Validator;

/**
 * @author wilj
 *
 */
public class ValidationTrigger<T> implements BlurHandler, ValueChangeHandler<T>, SelectionHandler<T> {
	@SuppressWarnings("unchecked")
	public static boolean bind(final Validator validator, final Object source) {
		boolean result = false;
		final ValidationTrigger trigger = new ValidationTrigger(validator);

		if (source instanceof HasBlurHandlers) {
			((HasBlurHandlers) source).addBlurHandler(trigger);
			result = true;
		}

		if (source instanceof HasValueChangeHandlers) {
			((HasValueChangeHandlers) source).addValueChangeHandler(trigger);
			result = true;
		}

		if (source instanceof HasSelectionHandlers) {
			((HasSelectionHandlers) source).addSelectionHandler(trigger);
			result = true;
		}

		return result;
	}

	private final Validator validator;

	private ValidationTrigger(final Validator validator) {
		this.validator = validator;
	}

	private void doValidate() {
		DeferredCommand.addCommand(new Command() {
			@Override
			public void execute() {
				validator.validate();
			}
		});
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.event.dom.client.BlurHandler#onBlur(com.google.gwt.event.dom.client.BlurEvent)
	 */
	@Override
	public void onBlur(final BlurEvent event) {
		doValidate();
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.event.logical.shared.SelectionHandler#onSelection(com.google.gwt.event.logical.shared.SelectionEvent)
	 */
	@Override
	public void onSelection(final SelectionEvent<T> event) {
		doValidate();
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(com.google.gwt.event.logical.shared.ValueChangeEvent)
	 */
	@Override
	public void onValueChange(final ValueChangeEvent<T> event) {
		doValidate();
	}

}
